//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureNewForm.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Feb 26 2009 07:30:56  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.form;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.struct.CostStructureCreateStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class CostStructureNewForm extends ValidatorForm {
    private String section;
    private String description;
    private String supplierTypeSel;
    private Collection supplierTypeCol;
    private String supplierCode;
    private String lookupedSupplier;
    private String commoditySel;
    private Collection commoditiesCol;
    private String partClassSel;
    private Collection partClassesCol;
    private String partPrefix;
    private String partBase;
    private String partSufix;
    private String lookupedPart;
    private String dateIn;
    private String dateOut;
    private String observations;
    private String createdBy;
    private String createdDate;
    private String proposedBy;
    private String proposedDate;
    private String approvedBy;
    private String approvedDate;
    private String lastModifiedBy;
    private String lastModifiedDate;
    private Collection typesCostStructureCol;
    private String costStructureTypeSel;
    private String version;
    private String blockAppliesTo;
    private Teesc02Coststvers costStrVersion;
    private String comeFrom;

    public void resetForm() {
        this.section = null;
        this.description = null;
        this.supplierTypeSel = null;
        this.supplierTypeCol = null;
        this.supplierCode = null;
        this.lookupedSupplier = null;
        this.commoditySel = null;
        this.commoditiesCol = null;
        this.partClassSel = null;
        this.partClassesCol = null;
        this.partPrefix = null;
        this.partBase = null;
        this.partSufix = null;
        this.lookupedPart = null;
        this.dateIn = null;
        this.dateOut = null;
        this.observations = null;
        this.createdBy = null;
        this.createdDate = null;
        this.proposedBy = null;
        this.proposedDate = null;
        this.approvedBy = null;
        this.approvedDate = null;
        this.lastModifiedBy = null;
        this.lastModifiedDate = null;
        this.typesCostStructureCol = null;
        this.costStructureTypeSel = null;
        this.version = null;
        this.blockAppliesTo = "false";
        this.costStrVersion = null;
        this.comeFrom = null;
    }

    public String getApprovedBy() {
        return approvedBy;
    }

    public void setApprovedBy(String approvedBy) {
        this.approvedBy = approvedBy;
    }

    public String getApprovedDate() {
        return approvedDate;
    }

    public void setApprovedDate(String approvedDate) {
        this.approvedDate = approvedDate;
    }

    public Collection getCommoditiesCol() {
        return commoditiesCol;
    }

    public void setCommoditiesCol(Collection commoditiesCol) {
        this.commoditiesCol = commoditiesCol;
    }

    public String getCommoditySel() {
        return commoditySel;
    }

    public void setCommoditySel(String commoditySel) {
        this.commoditySel = commoditySel;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getDateIn() {
        return dateIn;
    }

    public void setDateIn(String dateIn) {
        this.dateIn = dateIn;
    }

    public String getDateOut() {
        return dateOut;
    }

    public void setDateOut(String dateOut) {
        this.dateOut = dateOut;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getLookupedPart() {
        return lookupedPart;
    }

    public void setLookupedPart(String lookupedPart) {
        this.lookupedPart = lookupedPart;
    }

    public String getLookupedSupplier() {
        return lookupedSupplier;
    }

    public void setLookupedSupplier(String lookupedSupplier) {
        this.lookupedSupplier = lookupedSupplier;
    }

    public String getObservations() {
        return observations;
    }

    public void setObservations(String observations) {
        this.observations = observations;
    }

    public String getPartBase() {
        return partBase;
    }

    public void setPartBase(String partBase) {
        this.partBase = partBase;
    }

    public Collection getPartClassesCol() {
        return partClassesCol;
    }

    public void setPartClassesCol(Collection partClassesCol) {
        this.partClassesCol = partClassesCol;
    }

    public String getPartClassSel() {
        return partClassSel;
    }

    public void setPartClassSel(String partClassSel) {
        this.partClassSel = partClassSel;
    }

    public String getPartPrefix() {
        return partPrefix;
    }

    public void setPartPrefix(String partPrefix) {
        this.partPrefix = partPrefix;
    }

    public String getPartSufix() {
        return partSufix;
    }

    public void setPartSufix(String partSufix) {
        this.partSufix = partSufix;
    }

    public String getProposedBy() {
        return proposedBy;
    }

    public void setProposedBy(String proposedBy) {
        this.proposedBy = proposedBy;
    }

    public String getProposedDate() {
        return proposedDate;
    }

    public void setProposedDate(String proposedDate) {
        this.proposedDate = proposedDate;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    public Collection getSupplierTypeCol() {
        return supplierTypeCol;
    }

    public void setSupplierTypeCol(Collection supplierTypeCol) {
        this.supplierTypeCol = supplierTypeCol;
    }

    public String getSupplierTypeSel() {
        return supplierTypeSel;
    }

    public void setSupplierTypeSel(String supplierTypeSel) {
        this.supplierTypeSel = supplierTypeSel;
    }

    public Collection getTypesCostStructureCol() {
        return typesCostStructureCol;
    }

    public void setTypesCostStructureCol(Collection typesCostStructureCol) {
        this.typesCostStructureCol = typesCostStructureCol;
    }

    public String getCostStructureTypeSel() {
        return costStructureTypeSel;
    }

    public void setCostStructureTypeSel(String costStructureTypeSel) {
        this.costStructureTypeSel = costStructureTypeSel;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public CostStructureCreateStruct getCostStructureCreateStruct() {
        CostStructureCreateStruct ret = new CostStructureCreateStruct();

        ret.setCommodityNumber(this.getCommoditySel());
        ret.setCostStructureType(this.getCostStructureTypeSel());
        ret.setDateIn(this.getDateIn());
        if (!StringUtils.isBlank(this.getDateOut()))
            ret.setDateOut(this.getDateOut());
        else
            ret.setDateOut(FullEconomicsDateUtil.maxDateStr());
        ret.setDescription(this.getDescription());
        ret.setObservations(this.getObservations());
        ret.setPartClass(this.getPartClassSel());
        ret.setPartPrefix(this.getPartPrefix());
        ret.setPartBase(this.getPartBase());
        ret.setPartSufix(this.getPartSufix());
        ret.setSupplierCode(this.getSupplierCode());
        ret.setSupplierType(this.getSupplierTypeSel());
        return ret;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getBlockAppliesTo() {
        return blockAppliesTo;
    }

    public void setBlockAppliesTo(String blockAppliesTo) {
        this.blockAppliesTo = blockAppliesTo;
    }

    /**
     * Setea los detalles de la pantalla
     * 
     */
    public void setDetails() {
        Teesp01Part part = null;
        GregorianCalendar calendar = new GregorianCalendar();
        Integer version = null;

        if ("newVersion".equals(this.comeFrom)) {
            version = costStrVersion.getEesc02CststvVersionNumberK() + 1;
        } else {
            version = costStrVersion.getEesc02CststvVersionNumberK();
        }

        this.setVersion(version.toString());
        this.setDescription(costStrVersion.getTeesc03Subcoststr()
                .getEesc03SubcstNameN());
        this.setSupplierTypeSel(costStrVersion.getTeesc01Coststruct()
                .getTeess01Supplier().getEess01SupplierClassCodeC());
        this.setSupplierCode(costStrVersion.getTeesc01Coststruct()
                .getTeess01Supplier().getEess01SupplierCodeC());
        this.setCostStructureTypeSel(costStrVersion.getTeesc01Coststruct()
                .getEesc01CststrTypeCodeC());
        if (costStrVersion.getTeesc01Coststruct().getEesc01CststrTypeCodeC()
                .equals("C")) {
            this.setCommoditySel(costStrVersion.getTeesc01Coststruct()
                    .getTeesp02Commodity().getEesp02CommodityNumberK()
                    .toString());
        } else if (costStrVersion.getTeesc01Coststruct()
                .getEesc01CststrTypeCodeC().equals("P")) {
            part = costStrVersion.getTeesc01Coststruct().getTeesp04Supply()
                    .getTeesp01Part();
            this.setPartClassSel(part.getEesp01PartClassCodeC());
            this.setPartPrefix(part.getEesp01PartPrefixCodeC());
            this.setPartBase(part.getEesp01PartBaseCodeC());
            this.setPartSufix(part.getEesp01PartSuffixCodeC());
        }
        calendar.setTime(costStrVersion.getEesc02CststvInDateY());
        this.setDateIn(FullEconomicsDateUtil.format(calendar));
        calendar.setTime(costStrVersion.getEesc02CststvOutDateY());
        this.setDateOut(FullEconomicsDateUtil.format(calendar));
        this.setObservations(costStrVersion.getEesc02CststvCommentsX());

        if (!"newVersion".equals(this.getComeFrom())
                && !"copyTo".endsWith(this.getComeFrom())) {
            if (costStrVersion.getEesc02CreateUserC() != null) {
                this.createdBy = costStrVersion.getEesc02CreateUserC();
                calendar.setTime(costStrVersion.getEesc02CreateS());
                this.setCreatedDate(FullEconomicsDateUtil.format(calendar));
            }
            if (costStrVersion.getEesc02CststvProposalUserC() != null) {
                this.proposedBy = costStrVersion.getEesc02CststvProposalUserC();
                calendar.setTime(costStrVersion.getEesc02CststvProposalS());
                this.setProposedDate(FullEconomicsDateUtil.format(calendar));
            }
            if (costStrVersion.getEesc02CststvApprovalUserC() != null) {
                this.approvedBy = costStrVersion.getEesc02CststvApprovalUserC();
                calendar.setTime(costStrVersion.getEesc02CststvApprovalS());
                this.setApprovedDate(FullEconomicsDateUtil.format(calendar));
            }
            if (costStrVersion.getEesc02LastUpdtUserC() != null) {
                this.lastModifiedBy = costStrVersion.getEesc02LastUpdtUserC();
                calendar.setTime(costStrVersion.getEesc02LastUpdtS());
                this
                        .setLastModifiedDate(FullEconomicsDateUtil
                                .format(calendar));
            }
        }
    }

    /**
     * Devuelve la Cost Structure Version con los valores que modifico el
     * usuario
     * 
     * @return
     */
    public Teesc02Coststvers getCostStrForNewVersion() {
        Calendar cal = null;

        cal = FullEconomicsDateUtil.format(this.getDateIn());
        costStrVersion.setEesc02CststvInDateY(new Timestamp(cal
                .getTimeInMillis()));
        cal = FullEconomicsDateUtil.format(this.getDateOut());
        costStrVersion.setEesc02CststvOutDateY(new Timestamp(cal
                .getTimeInMillis()));
        costStrVersion.setEesc02CststvCommentsX(this.getObservations());
        costStrVersion.getTeesc03Subcoststr().setEesc03SubcstNameN(
                this.getDescription());

        return costStrVersion;
    }

    public Teesc02Coststvers getCostStrVersion() {
        return costStrVersion;
    }

    public void setCostStrVersion(Teesc02Coststvers costStrVersion) {
        this.costStrVersion = costStrVersion;
    }

    public String getComeFrom() {
        return comeFrom;
    }

    public void setComeFrom(String comeFrom) {
        this.comeFrom = comeFrom;
    }

    /**
     * Devuelve la Version de estructura de costos para hacer update
     * 
     * @return
     */
    public Teesc02Coststvers getCostStrVersionForUpdate() {
        Calendar cal = null;

        cal = FullEconomicsDateUtil.format(this.getDateIn());
        costStrVersion.setEesc02CststvInDateY(new Timestamp(cal
                .getTimeInMillis()));
        cal = FullEconomicsDateUtil.format(this.getDateOut());
        costStrVersion.setEesc02CststvOutDateY(new Timestamp(cal
                .getTimeInMillis()));
        costStrVersion.setEesc02CststvCommentsX(this.getObservations());
        costStrVersion.getTeesc03Subcoststr().setEesc03SubcstNameN(
                this.getDescription());

        return costStrVersion;
    }

    /**
     * 
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        Calendar calIn = null;
        Calendar calOut = null;

        // Valido solo si no viene de clickear en Cabecera en el listado
        if (!this.getComeFrom().equals("header")) {
            if (StringUtils.isBlank(this.getDescription())) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.required",
                                        resources
                                                .getMessage("costStructure.new.label.description")));
            }
            if (this.getCostStructureTypeSel().equals("-1")) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "error.noSelected.m",
                                        resources
                                                .getMessage("costStructure.new.label.appliesTo")));
            } else if (this.getCostStructureTypeSel().equalsIgnoreCase("S")) {
                if (this.getSupplierTypeSel().equals("-1")
                        || StringUtils.isBlank(this.getSupplierCode())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("costStructure.new.label.supplier")));
                }
            } else if (this.getCostStructureTypeSel().equalsIgnoreCase("C")) {
                if (this.getCommoditySel().equals("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.required", resources
                                    .getMessage("commodity.label")));
                }
            } else if (this.getCostStructureTypeSel().equalsIgnoreCase("P")) {
                if (this.getPartClassSel().equals("-1")
                        || StringUtils.isBlank(this.getPartPrefix())
                        || StringUtils.isBlank(this.getPartBase())
                        || StringUtils.isBlank((this.getPartSufix()))) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.required", resources
                                    .getMessage("part.label")));
                }
            }
            calIn = FullEconomicsDateUtil.format(this.getDateIn());
            if (calIn == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.invalid", resources
                                .getMessage("costStructure.new.label.dateIn")));
            }
            if (!StringUtils.isBlank(this.getDateOut())) {
                calOut = FullEconomicsDateUtil.format(this.getDateOut());
                if (calOut == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.invalid",
                                            resources
                                                    .getMessage("costStructure.new.label.dateOut")));
                }
            }
            if (calIn != null && calOut != null) {
                if (calIn.after(calOut)) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "costStructure.new.error.dateInGTDateOut"));
                }
            }
        }

        return ae;
    }

    /**
     * Metodo auxiliar
     * 
     * @return
     */
    public Teess01Supplier getSupplier() {
        Teess01Supplier supplier = new Teess01Supplier();

        supplier.setEess01SupplierClassCodeC(this.getSupplierTypeSel());
        supplier.setEess01SupplierCodeC(this.getSupplierCode());

        return supplier;
    }

    /**
     * Metodo auxiliar
     * 
     * @return
     */
    public Teesp01Part getPart() {
        Teesp01Part part = new Teesp01Part();

        part.setEesp01PartClassCodeC(this.getPartClassSel());
        part.setEesp01PartPrefixCodeC(this.getPartPrefix());
        part.setEesp01PartBaseCodeC(this.getPartBase());
        part.setEesp01PartSuffixCodeC(this.getPartSufix());

        return part;
    }

}
